<template>
  <div class="containers">
    <el-container style="align-items: stretch">
      <el-main class="flow-viewer">
        <div class="process-status">
          <span class="intro">状态：</span>
          <div class="finish">已办理</div>
          <div class="processing">处理中</div>
          <div class="todo">未进行</div>
        </div>
        <!-- 流程图显示 -->
        <div ref="bpmn-canvas" v-loading="loading" class="canvas"></div>
        <!--  按钮区域  -->
        <el-button-group class="button-group">
          <el-tooltip content="适中" effect="dark" placement="bottom">
            <el-button :icon="Rank" size="small" @click="fitViewport" />
          </el-tooltip>
          <el-tooltip content="放大" effect="dark" placement="bottom">
            <el-button :icon="ZoomIn" size="small" @click="zoomViewport(true)" />
          </el-tooltip>
          <el-tooltip content="缩小" effect="dark" placement="bottom">
            <el-button :icon="ZoomOut" size="small" @click="zoomViewport(false)" />
          </el-tooltip>
        </el-button-group>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import BpmnViewer from 'bpmn-js/lib/Viewer';
import { Rank, ZoomIn, ZoomOut } from '@element-plus/icons';
export default {
  name: 'BpmnViewer',
  setup() {
    return {
      Rank,
      ZoomIn,
      ZoomOut
    };
  },
  /** 组件传值  */
  props: {
    // 回显数据传值
    flowData: {
      type: Object,
      default: () => {},
      required: false
    },
    procInsId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      bpmnViewer: null,
      flowDetail: {},
      loading: true
    };
  },
  /** 传值监听 */
  watch: {
    flowData: {
      handler(newValue) {
        if (Object.keys(newValue).length > 0) {
          // 生成实例
          this.bpmnViewer && this.bpmnViewer.destroy();
          this.bpmnViewer = new BpmnViewer({
            container: this.$refs['bpmn-canvas']
          });
          this.loadFlowCanvas(newValue);
        }
      }
    }
  },
  created() {},
  methods: {
    // 加载流程图片
    async loadFlowCanvas(flowData) {
      try {
        await this.bpmnViewer.importXML(flowData.xmlData);
        await this.fitViewport();
        // 流程线高亮设置
        if (flowData.nodeData !== undefined && flowData.nodeData.length > 0) {
          await this.fillColor(flowData.nodeData);
        }
      } catch (err) {
        console.error(err.message, err.warnings);
      }
    },

    // 让图能自适应屏幕
    fitViewport() {
      this.zoom = this.bpmnViewer.get('canvas').zoom('fit-viewport', 'auto');
      this.loading = false;
    },

    // 放大缩小
    zoomViewport(zoomIn = true) {
      this.zoom = this.bpmnViewer.get('canvas').zoom();
      this.zoom += zoomIn ? 0.1 : -0.1;
      if (this.zoom >= 0.2) this.bpmnViewer.get('canvas').zoom(this.zoom);
    },

    // 设置高亮颜色的
    fillColor(nodeData) {
      const canvas = this.bpmnViewer.get('canvas');
      this.bpmnViewer.getDefinitions().rootElements[0].flowElements.forEach(n => {
        const completeTask = nodeData.find(m => m.key === n.id);
        const todoTask = nodeData.find(m => !m.completed);
        const endTask = nodeData[nodeData.length - 1];
        if (n.$type === 'bpmn:UserTask') {
          if (completeTask) {
            canvas.addMarker(n.id, completeTask.completed ? 'highlight' : 'highlight-todo');
            n.outgoing?.forEach(nn => {
              const targetTask = nodeData.find(m => m.key === nn.targetRef.id);
              if (targetTask) {
                if (todoTask && completeTask.key === todoTask.key && !todoTask.completed) {
                  canvas.addMarker(nn.id, todoTask.completed ? 'highlight' : 'highlight-todo');
                  canvas.addMarker(nn.targetRef.id, todoTask.completed ? 'highlight' : 'highlight-todo');
                } else {
                  canvas.addMarker(nn.id, targetTask.completed ? 'highlight' : 'highlight-todo');
                  canvas.addMarker(nn.targetRef.id, targetTask.completed ? 'highlight' : 'highlight-todo');
                }
              }
            });
          }
        }
        // 排他网关
        else if (n.$type === 'bpmn:ExclusiveGateway') {
          if (completeTask) {
            canvas.addMarker(n.id, completeTask.completed ? 'highlight' : 'highlight-todo');
            n.outgoing?.forEach(nn => {
              const targetTask = nodeData.find(m => m.key === nn.targetRef.id);
              if (targetTask) {
                canvas.addMarker(nn.id, targetTask.completed ? 'highlight' : 'highlight-todo');
                canvas.addMarker(nn.targetRef.id, targetTask.completed ? 'highlight' : 'highlight-todo');
              }
            });
          }
        }
        // 并行网关
        else if (n.$type === 'bpmn:ParallelGateway') {
          if (completeTask) {
            canvas.addMarker(n.id, completeTask.completed ? 'highlight' : 'highlight-todo');
            n.outgoing?.forEach(nn => {
              const targetTask = nodeData.find(m => m.key === nn.targetRef.id);
              if (targetTask) {
                canvas.addMarker(nn.id, targetTask.completed ? 'highlight' : 'highlight-todo');
                canvas.addMarker(nn.targetRef.id, targetTask.completed ? 'highlight' : 'highlight-todo');
              }
            });
          }
        } else if (n.$type === 'bpmn:StartEvent') {
          n.outgoing.forEach(nn => {
            const completeTask = nodeData.find(m => m.key === nn.targetRef.id);
            if (completeTask) {
              canvas.addMarker(nn.id, 'highlight');
              canvas.addMarker(n.id, 'highlight');
            }
          });
        } else if (n.$type === 'bpmn:EndEvent') {
          if (endTask.key === n.id && endTask.completed) {
            canvas.addMarker(n.id, 'highlight');
          }
        }
      });
    }
  }
};
</script>

<style lang="scss">
.my-header {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.flowMsgPopover {
  display: none;
}

.tipBox {
  width: 180px;
  background: #fff;
  border-radius: 4px;
  border: 1px solid #ebeef5;
  padding: 12px;

  p {
    line-height: 28px;
    margin: 0;
    padding: 0;
  }
}

.cell-item {
  display: flex;
  align-items: center;
}

// bpmn 画布 logo
//.bjs-powered-by {
//  display: none;
//}
.view-mode {
  .el-header,
  .el-aside,
  .djs-palette,
  .bjs-powered-by {
    display: none;
  }

  .el-loading-mask {
    background-color: initial;
  }

  .el-loading-spinner {
    display: none;
  }
}

.containers {
  width: 100%;
  height: 100%;

  .canvas {
    width: 100%;
    height: 100%;
    background: url('');
  }

  .panel {
    position: absolute;
    right: 0;
    top: 50px;
    width: 300px;
  }

  .load {
    margin-right: 10px;
  }

  .djs-palette {
    left: 0px !important;
    top: 0px;
    border-top: none;
  }

  .djs-container svg {
    min-height: 650px;
  }

  .overlays-div {
    font-size: 10px;
    color: red;
    width: 100px;
    top: -20px !important;
  }

  .flow-viewer {
    position: relative;
    padding: 0;
  }

  .flow-viewer .button-group {
    display: flex;
    position: absolute;
    width: auto;
    height: auto;
    top: 10px;
    right: 10px;
  }

  // 流程线
  .highlight.djs-shape .djs-visual > :nth-child(1) {
    fill: #56bb56 !important;
    stroke: #56bb56 !important;
    fill-opacity: 0.2 !important;
  }

  .highlight.djs-shape .djs-visual > :nth-child(2) {
    fill: #56bb56 !important;
  }

  .highlight.djs-shape .djs-visual > path {
    fill: #56bb56 !important;
    fill-opacity: 0.2 !important;
    stroke: #56bb56 !important;
  }

  .highlight.djs-connection > .djs-visual > path {
    stroke: #56bb56 !important;
  }

  .highlight-todo.djs-connection > .djs-visual > path {
    stroke: #eab24a !important;
    stroke-dasharray: 4px !important;
    fill-opacity: 0.2 !important;
  }

  .highlight-todo.djs-shape .djs-visual > :nth-child(1) {
    stroke-dasharray: 5, 5;
    stroke-dashoffset: 500;
    stroke: #eab24a !important;
    fill: rgba(252, 211, 127, 0.2) !important;
  }

  @keyframes draw {
    100% {
      stroke-dashoffset: 0;
    }
  }

  .process-status {
    position: absolute;
    width: auto;
    height: auto;

    display: flex;
    float: right;
    top: 10px;
    left: 10px;
    font-size: 12px;

    .intro {
      color: #303133;
      margin-top: 5px;
    }

    .finish {
      background-color: #e8ffea;
      padding: 4px;
      border: 1px solid rgba(0, 180, 42, 0.1);
      border-radius: 3px;
      color: #56bb56;
      margin-right: 8px;
    }

    .processing {
      background-color: #fcf5ea;
      padding: 4px;
      border: 1px solid #fce9c7;
      border-radius: 3px;
      color: #eab24a;
      margin-right: 8px;
    }

    .todo {
      padding: 4px;
      background: #ecedee;
      border: 1px solid rgba(204, 204, 204, 0.1);
      border-radius: 3px;
      color: #666666;
      margin-right: 5px;
    }
  }
}
</style>
